/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  AccountGroupHelper.java                                      
 *  File Contents Outline :  用户组持久化类
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */

package storage.dbhelper;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.LinkedList;
import java.util.List;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.KeySlice;
import storage.dbobject.DbAccountGroup;
import storage.util.StorageManager;
import cn.edu.thu.log.web.service.PropertiesReaderServices;
import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;

/**
 *
 * @author bloodyparadise
 */
public class AccountGroupHelper {
    /**
     * 
     * @param accountGroup 
     */
    public void saveAccountGroup(DbAccountGroup accountGroup){
        PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_accountGroup"));
        sm.setRowKey(accountGroup.getRowKey());
        //sm.open();
        sm.put(DbAccountGroup.cl_groupName, accountGroup.getGroupName());
        sm.put(DbAccountGroup.cl_groupId, accountGroup.getGroupId());
        //sm.close();
    }
    
    public List<DbAccountGroup> getAccountGroups(final String columnName, final String value) {
        List<DbAccountGroup> result = new LinkedList<DbAccountGroup>();
    PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_accountGroup"));
        //sm.open();
        List<KeySlice> crs = sm.get(columnName, value);
        try {
            for (KeySlice ks : crs) {
                String rowKey = new String(ks.getKey());
                String groupName=null,groupId=null;
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbAccountGroup.cl_groupName)) {
                        groupName = columnsValue;
                    } else if (columnsName.equals(DbAccountGroup.cl_groupId)) {
                        groupId = columnsValue;
                    } 
                }
                DbAccountGroup accountGroup = new DbAccountGroup(rowKey, groupName, groupId);
                result.add(accountGroup);
            }
        } catch (Exception ex) {
            Logger.getLogger(AccountGroupHelper.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            //sm.close();
        }
        return result;
    }
    
    public void deleteAccountGroup(final String rowKey){
      PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_accountGroup"));
        //sm.open();
        sm.delete(rowKey);
        //sm.close();
    }
}
